﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;
using DAO;
namespace BUS
{
    public class LoaiPhongBUS
    {
        public const int trungTen = 0;
        public const int thatBai = 1;
        public const int thanhCong = 2;


        public static List<LoaiPhongDTO> LayDSLoaiPhong(int maLoaiPhong = -1, string tenLoaiPhong = null)
        {
            return LoaiPhongDAO.LayDSLoaiPhong(maLoaiPhong, tenLoaiPhong);
        }
        public static int ThemLoaiPhong(LoaiPhongDTO loaiPhong)
        {
            List<LoaiPhongDTO> temp = LayDSLoaiPhong(-1, loaiPhong.TenLoaiPhong);
            if (temp.Count > 0)
                return trungTen;
            if (!LoaiPhongDAO.ThemLoaiPhong(loaiPhong))
                return thatBai;
            return thanhCong;
        }
        public static int SuaLoaiPhong(LoaiPhongDTO loaiPhong)
        {
            List<LoaiPhongDTO> temp = LayDSLoaiPhong(-1, loaiPhong.TenLoaiPhong);
            if (temp.Count > 0)
                return trungTen;
            if (!LoaiPhongDAO.SuaLoaiPhong(loaiPhong))
                return thatBai;
            return thanhCong;
        }
        public static bool XoaLoaiPhong(int _maLoaiPhong)
        {
            if ((LoaiPhongDAO.XoaLoaiPhong(_maLoaiPhong)))// neu xoa loai phong thanh cong, xoa cac phan lien quan
            {
                //xoa phieu dat phong
                List<PhieuDatPhongDTO> DSPhieuDat = PhieuDatPhongBUS.LayDSPhieuDatPhong(-1, null, -1, _maLoaiPhong);
                if (DSPhieuDat != null && DSPhieuDat.Count > 0)
                {
                    foreach (var item in DSPhieuDat)
                        if (!PhieuDatPhongBUS.XoaPhieuDatPhong(item.MaPhieuDatPhong))
                            return false;
                }
                //Xoa phong
                List<PhongDTO> DSPhong = PhongBUS.LayDSPhong(-1, null, -1, _maLoaiPhong);
                if (DSPhong != null && DSPhong.Count > 0)
                {
                    foreach (var item in DSPhong)
                        if (!PhongBUS.XoaPhong(item.MaPhong))
                            return false;
                }

                return true; ;
            }

            return false;
        }
    }
}
